package com.someclip.component.container
{
	import com.someclip.component.interfaces.IComponent;
	import com.someclip.component.interfaces.IContainer;
	import com.someclip.component.interfaces.ITile;
	import com.someclip.utils.drag.core.IDragObjectAcceptAble;

	import flash.display.DisplayObject;
	import flash.filters.DisplacementMapFilter;

	public class Tile extends Container implements ITile, IContainer, IDragObjectAcceptAble
	{
		private var _cellWidth:Number;
		private var _cellHeight:Number;
		private var _hGap:Number=1;
		private var _vGap:Number=1;
		private var _seted:Boolean=false;
		private var _maxWidth:Number=0;
		private var _maxHeight:Number=0;

		public function Tile()
		{
			this.width=150;
			this.height=150;
		}

		private function updateChilds():void
		{
			if (childList.length == 0)
				return;
			var tempW:Number;
			var tempH:Number;
			var countW:uint;
			var countH:uint;
			if (_seted)
			{
				tempW=_cellWidth;
				tempH=_cellHeight;
			}
			else
			{
				tempW=_maxWidth;
				tempH=_maxHeight;
			}
			countW=this.width / (tempW + _hGap);
			if (countW == 0)
				countW=1;
			var len:uint=childList.length;
			for (var i:uint=0; i < len; i++)
			{
				var child:DisplayObject=childList[i].children as DisplayObject;
				child.x=(i % countW + 1) * _hGap + (i % countW) * tempW;
				child.y=(uint(i / countW) + 1) * _vGap + uint(i / countW) * tempH;
			}
		}

		public function get cellWidth():Number
		{
			return _cellWidth;
		}

		public function set cellWidth(value:Number):void
		{
			if (isNaN(value))
				return;
			_cellWidth=value;
			_seted=!isNaN(_cellHeight);
		}

		public function get cellHeight():Number
		{
			return _cellHeight;
		}

		public function set cellHeight(value:Number):void
		{
			if (isNaN(value))
				return;
			_cellHeight=value;
			_seted=!isNaN(_cellWidth);
		}

		public function get hGap():Number
		{
			return _hGap;
		}

		public function set hGap(value:Number):void
		{
			if (isNaN(value))
				return;
			_hGap=value;
		}

		public function get vGap():Number
		{
			return _vGap;
		}

		public function set vGap(value:Number):void
		{
			if (isNaN(value))
				return;
			_vGap=value;
		}

		override public function addChild(child:DisplayObject):DisplayObject
		{
			if (child is IComponent)
			{
				(child as IComponent).top=NaN;
				(child as IComponent).bottom=NaN;
				(child as IComponent).vCenter=NaN;
				(child as IComponent).left=NaN;
				(child as IComponent).right=NaN;
				(child as IComponent).hCenter=NaN;
			}
			super.addChild(child);
			if (child.width > _maxWidth)
			{
				_maxWidth=child.width;
			}
			if (child.height > _maxHeight)
			{
				_maxHeight=child.height;
			}
			updateChilds();
			return child;
		}

		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if (child is IComponent)
			{
				(child as IComponent).top=NaN;
				(child as IComponent).bottom=NaN;
				(child as IComponent).vCenter=NaN;
				(child as IComponent).left=NaN;
				(child as IComponent).right=NaN;
				(child as IComponent).hCenter=NaN;
			}
			super.addChildAt(child, index);
			if (child.width > _maxWidth)
			{
				_maxWidth=child.width;
			}
			if (child.height > _maxHeight)
			{
				_maxHeight=child.height;
			}
			updateChilds();
			return child;
		}

		override public function removeChild(child:DisplayObject):DisplayObject
		{
			super.removeChild(child);
			if (childList.length > 0)
			{
				_maxWidth=0;
				_maxHeight=0;
				for each (var c:DisplayObject in childList)
				{
					if (c.width > _maxWidth)
					{
						_maxWidth=c.width;
					}
					if (c.height > _maxHeight)
					{
						_maxHeight=c.height;
					}
					c=null;
				}
			}
			updateChilds();
			return child;
		}

		override public function removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject=super.removeChildAt(index);
			if (childList.length > 0)
			{
				_maxWidth=0;
				_maxHeight=0;
				for each (var c:DisplayObject in childList)
				{
					if (c.width > _maxWidth)
					{
						_maxWidth=c.width;
					}
					if (c.height > _maxHeight)
					{
						_maxHeight=c.height;
					}
					c=null;
				}
			}
			updateChilds();
			return child;
		}
	}
}
